﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Unikado.PHPLibs.Tokenizer.Highlighter
{

    /// <summary>
    /// A HighlighterTypeConfig collection.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Usage is realy simple. You can already access a element by <c>this[type]</c>.
    /// If the config is not defined for the type it will be created automatically.
    /// </para>
    /// </remarks>
    public class HighlighterTypeConfigs : IEnumerable<HighlighterTypeConfig>
    {

        #region - - - - - -   P R I V A T E   F I E L D S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private Dictionary<HighlighterTokenType, HighlighterTypeConfig> data;

        #endregion

        #region - - - - - -   P U B L I C   P R O P E R T I E S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        /// <summary>
        /// Returns the Config for current defined type. If no config is defined for the type, a default one ist created.
        /// </summary>
        /// <param name="type">The Type</param>
        /// <returns>HighlighterTypeConfig</returns>
        public HighlighterTypeConfig this[HighlighterTokenType type]
        {
            get
            {
                if (!data.ContainsKey(type)) data.Add(type, new HighlighterTypeConfig(type, "#000"));
                return data[type];
            }
        }
        /// <summary>
        /// Returns the Types of all current defined configs.
        /// </summary>
        public IEnumerable<HighlighterTokenType> Types
        {
            get { return data.Keys; }
        }
        /// <summary>
        /// Returns how many configs are currently assigned
        /// </summary>
        public int Count { get { return data.Count; } }

        #endregion

        #region - - - - - -   C O N S T R U C T O R S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        /// <summary>
        /// Inits a new instance.
        /// </summary>
        public HighlighterTypeConfigs()
        {
            data = new Dictionary<HighlighterTokenType, HighlighterTypeConfig>();
        }

        #endregion

        #region - - - - - -   P U B L I C   M E T H O D S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        /// <summary>
        /// Returns a enumerator for looping all elements
        /// </summary>
        /// <returns>Enumerator</returns>
        public IEnumerator<HighlighterTypeConfig> GetEnumerator()
        {
            return data.Select(config => config.Value).GetEnumerator();
        }
        /// <summary>
        /// Writes the whole CSS rules, used by this configs. (Usable inside a style element or in a CSS file)
        /// </summary>
        /// <param name="sb">Here the rules will be written.</param>
        public void WriteCssRules(ref StringBuilder sb)
        {
            foreach (var cfg in this) sb.Append(cfg.ToCssRule());
        }
        /// <summary>
        ///  Writes the token (as HTML span or a element) with the current matching config, if the token type matches the current defined config types.
        /// </summary>
        /// <param name="token">The Token</param>
        /// <param name="sb">Here is written if the Method returns true.</param>
        /// <returns>bool</returns>
        public bool WriteHtml(Token token, ref StringBuilder sb)
        {
            foreach (var cnf in this)
            {
                if (cnf.WriteHtml(token, ref sb)) return true;
            }
            return false;
        }

        #endregion

        #region - - - - - -   I N T E R N A L   M E T H O D S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

    }

}